package controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.TreeMap;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import data.AutoSiteData;
import data.BlogData;
import data.UserData;
import debugs.AutoSiteDebug;
import forms.AutoSiteForm;

import models.AutoSite;
import models.Blog;
import models.User;

public class AddSiteServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		String gmail = request.getUserPrincipal().getName();
		
		//Get Blog Id
		String blogId = (String) request.getParameter("blogId");
		
		//Get list site in blog
		AutoSiteData aData = new AutoSiteData();
		ArrayList<AutoSite> sites = aData.getByBlogId(blogId);
		//Get blog info
		BlogData blogData = new BlogData();
		Blog blog = blogData.getById(blogId);
		
		AutoSite autoSite = new AutoSite();
		TreeMap check = new TreeMap();
		
		request.setAttribute("check", check);
		request.setAttribute("blog", blog);
		request.setAttribute("sites", sites);
		request.setAttribute("autoSite", autoSite);
		request.setAttribute("form", "add");
		try {
            request.getRequestDispatcher("/pages/form_site.jsp").forward(request, response);
        } catch (Exception e) {
            //TODO: handle exception
            e.printStackTrace();
        }
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		String gmail = request.getUserPrincipal().getName();
				
		//Get blogId
		String blogId = (String) request.getParameter("blogId");
		//Get list site in blog
		AutoSiteData aData = new AutoSiteData();
		
		//Get blog info
		BlogData blogData = new BlogData();
		Blog blog = blogData.getById(blogId);
		//Set site info
		AutoSite autoSite = new AutoSite();
		autoSite.setBlogId(blogId);
		//Get value form and check valid
		TreeMap form = AutoSiteForm.checkValid(request, autoSite);
		autoSite =(AutoSite) form.get("autoSite");
		TreeMap check = (TreeMap) form.get("checkValid");
		
		//Get all site in blog
		ArrayList<AutoSite> sites = aData.getByBlogId(blogId);
		
		//Check siteLimit in blog
		/*if (blog.getLimitSite() <= sites.size()) {
			check.put("limitSite", "Site has exceeded the number of permitted limits.");
		}*/
		
		//Save data
		if (check.size() == 0){
			autoSite.setId(autoSite.createId(autoSite.getBlogId(), autoSite.getSite()));
			aData.insert(autoSite);
			request.setAttribute("success", "Add success site " + autoSite.getId());
			sites = aData.getByBlogId(blogId);
			autoSite = new AutoSite();
		}
		
		request.setAttribute("check", check);
		request.setAttribute("blog", blog);
		request.setAttribute("sites", sites);
		request.setAttribute("autoSite", autoSite);
		request.setAttribute("form", "add");
		try {
            request.getRequestDispatcher("/pages/form_site.jsp").forward(request, response);
        } catch (Exception e) {
            //TODO: handle exception
            e.printStackTrace();
        }
	}
}